﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using GameViewer.Core.Archives;

namespace GameViewer.Core
{
    public partial class MainForm : DockContent
    {
        private string mAppDirectory;
        private Dictionary<string, DockContent> m_DockContents = new Dictionary<string, DockContent>();

        private static MainForm msInstance;
        public static MainForm Instance
        {
            get
            {
                return msInstance;
            }
        }

        public MainForm()
        {
            InitializeComponent();

            mAppDirectory = System.IO.Directory.GetCurrentDirectory();
            msInstance = this;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Core.Controls.ArchiveList.Instance.Show(this.dockPanel1, DockState.DockLeft);

            // LogManager
            LogManager.Instance.CreateLog(AppDirectory + @"\log.log", true, true, false);
            LogManager.Instance.LogMessage("GameViewer initialising.");

            // PluginManager
            new PluginManager();

            // ArchiveManager
            ArchiveManager.Instance.AddFactory(new Archives.FileSystemArchiveFactory());

            //this.SetViewer(new Viewers.MeshViewer());
            LogManager.Instance.LogMessage("GameViewer initialised OK.");
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.OpenArchive();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            this.OpenArchive();
        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            Core.Controls.ArchiveList.Instance.ExtractFile();
        }

        private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.toolStrip1.Visible = 
        }

        private void OpenArchive()
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in open.FileNames)
                {
                    Core.Controls.ArchiveList.Instance.List(ArchiveManager.Instance.Load(file));
                }
            }
        }

        private void OpenDir()
        {
            //l:\GameDev\Ogre\CeguiMeshViewer\Media\Ogre\models\
            string dir = @"l:\GameDev\Media\";
            if (System.IO.Directory.Exists(dir) == false)
            {
                dir = @"d:\Data\Ogre-Media\";
            }
            Core.Controls.ArchiveList.Instance.List(ArchiveManager.Instance.Load(dir));
            return;

            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                Core.Controls.ArchiveList.Instance.List(ArchiveManager.Instance.Load(folder.SelectedPath));
            }
        }

        private void toolsToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            this.toolStrip1.Visible = this.toolsToolStripMenuItem.Checked;
        }

        private void statusToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            this.statusStrip1.Visible = this.statusToolStripMenuItem.Checked;
        }

        public void AddDockContent(DockContent dc)
        {
            if (m_DockContents.ContainsKey(dc.Name) == false)
            {
                m_DockContents.Add(dc.Name, dc);
            }
        }

        public void SetViewer(Core.Viewers.Viewer viewer)
        {
            viewer.Show(this.dockPanel1, DockState.Document);
        }

        public string AppDirectory
        {
            get
            {
                return mAppDirectory;
            }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            this.OpenDir();
        }
    }
}
